package com.wskeee.ec.containers
{
	import flash.display.DisplayObject;
	
	import com.wskeee.ec.core.EComponent;

	public class Box extends Container
	{
		public static const HORIZONTAL:String = "horizontal";
		public static const VERTICAL:String = "vertical";
		
		protected var _direction:String = HORIZONTAL;
		
		/**
		 * 最大的 child.height 用于计算布局
		 */
		protected var maxItemHeight:Number = 0;
		/**
		 * 最大的 child.width 用于计算布局
		 */
		protected var maxItemWidth:Number = 0;
		/**
		 * 所有 child 的 width 总和 
		 */
		protected var contentWidth:Number = 0;
		/**
		 * 所有child 的 height 总和 
		 */
		protected var contentHeight:Number = 0;
		/**
		 * 旧测量宽度 
		 */
		protected var oldMeasureWidth:Number;
		/**
		 * 旧测量高度
		 */
		protected var oldMeasureHeight:Number;
		
		public function Box()
		{
			super();
		}
		
		//========================= validate ====================
		override protected function measure():void
		{
			oldMeasureWidth = measureWidth;
			oldMeasureHeight = measureHeight;
			measureWidth = measureHeight = 0;
			maxItemWidth = maxItemHeight = 0;
			contentWidth = contentHeight = 0;
			
			validateAllChild();
			var child:DisplayObject;
			for(var i:int=0,len:int=numChildren;i<len;i++)
			{
				child = getChildAt(i);
				contentWidth += child.width;
				contentHeight += child.height;
				if(child.height>maxItemHeight)
					maxItemHeight = child.height;
				if(child.width>maxItemWidth)
					maxItemWidth = child.width;
			}
			
		}
		//========================= end =========================
		
		//========================= utils =======================
		/**
		 * 使所有子对象都生效属性，特别是 width,height，主角为计算布局使用 
		 */
		protected function validateAllChild():void
		{
			var child:DisplayObject;
			for(var i:int = 0,len:int=numChildren; i<len; i++)
			{
				child = getChildAt(i);
				if(child is EComponent)
					EComponent(child).validateNow();
			}
		}
		//========================= end =========================
		
		//========================= get & set ===================
		/**
		 * 排序方向 
		 * @param value
		 * 
		 */
		public function set direction(value:String):void
		{
			_direction = value;
			invalidateDisplayList();
		}
		public function get direction():String
		{
			return _direction;
		}
		//========================= end =========================
	}
}